package com.example.jdai.jdai.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.DataLnfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ZmWebActivity extends Activity {

    @BindView(R.id.tb)
    TopBar tb;
    @BindView(R.id.wb_zm)
    WebView wbZm;
    @BindView(R.id.activity_zm_web)
    LinearLayout activityZmWeb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zm_web);
        ButterKnife.bind(this);
        tb.setTopBarLisener(new TopBar.TopBarLisener() {
            @Override
            public void back() {
                finish();
            }

            @Override
            public void more() {

            }
        });
        initWeb();
        wbZm.loadUrl(Config.ZIMA_WB+MyApplication.dataAll.data.get(DataAll.HAS_LOGIN));
    }

    @OnClick({R.id.tb, R.id.wb_zm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tb:
                break;
            case R.id.wb_zm:
                break;
        }
    }

    void initWeb(){
//        wbZm.getSettings().setJavaScriptEnabled(true);
//        wbZm.getSettings().setSupportZoom(true);
//        wbZm.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        wbZm.getSettings().setLoadWithOverviewMode(true);
//        wbZm.loadUrl(Config.ZIMA_WB+MyApplication.dataAll.data.get(DataAll.HAS_LOGIN));
//        wbZm.addJavascriptInterface(new JavaScriptinterface(this),
//                "zmxy_native");
//        wbZm.setWebViewClient(new WebViewClient(){
//            public boolean shouldOverrideUrlLoading(WebView view, String url)
//            { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
//                view.loadUrl(url);
//                return true;
//            }
//        });

        wbZm.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
        wbZm.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, SslError error) {
            }
        });
        wbZm.addJavascriptInterface(new JavaScriptinterface(this), "zmxy_native");
        setDefault();
    }


    @Override
    protected void onPause() {
        //DataAll.ZIMA_STATUS=DataAll.OK;
       // MyApplication.backRest.getZima(MyApplication.main);
        //finish();
        super.onPause();
    }
    public class JavaScriptinterface {
        Context context;
        public JavaScriptinterface(Context c) {
            context= c;
        }

        /**
         * 与js交互时用到的方法，在js里直接调用的
         */
        @JavascriptInterface
        public void callback(String ssss) {

            MyApplication.dataAll.data.put(DataAll.ZIMA_STATUS,DataAll.OK);
            Toast.makeText(MyApplication.applicationContext,"授权成功",Toast.LENGTH_SHORT).show();
            //DataAll.ZIMA_STATUS=DataAll.OK;
            MyApplication.dataAll.saveAll();
            MyApplication.startNext(ZmWebActivity.this);
            //MyApplication.backRest.getZima(MyApplication.main);
            finish();
        }
    }
    @SuppressLint("NewApi")
    private void setDefault() {
        WebSettings webSettings = wbZm.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // User settings

        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);// 关键点

        webSettings
                .setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
        webSettings.setSupportZoom(true); // 支持缩放

        webSettings.setLoadWithOverviewMode(true);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int mDensity = metrics.densityDpi;
        if (mDensity == 240) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == 160) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        } else if (mDensity == 120) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        } else if (mDensity == DisplayMetrics.DENSITY_XHIGH) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == DisplayMetrics.DENSITY_TV) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        }


        /**
         * 用WebView显示图片，可使用这个参数 设置网页布局类型： 1、LayoutAlgorithm.NARROW_COLUMNS ：
         * 适应内容大小 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
         */
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    }
    @Override
    protected void onDestroy() {
        if (wbZm != null) {
            wbZm.destroy();
        }
        super.onDestroy();
    }
}
